package com.t9101DR.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.t9101DR.bean.RoomAddress;
import com.t9101DR.bean.shopping;
import com.t9101DR.dao.BaseDao;
import com.t9101DR.dao.shoppingDao;

public class shoppingDaoImpl extends BaseDao implements shoppingDao{

	@Override
	public List<shopping> list(shopping s) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<shopping> list=new ArrayList<shopping>();
		try {
			conn=getConnection();
			String sql="SELECT shoppingId,name,price,quality,diamond,size,lettering,artText FROM `shopping` WHERE 1=1";
			if(s!=null&&s.getShoppingId()!=0) {
				sql=sql+" and shoppingId= "+s.getShoppingId()+" ";
			}
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				shopping bren=new shopping();
				bren.setShoppingId(rs.getInt(1));
				bren.setName(rs.getString(2));
				bren.setPrice(rs.getInt(3));
				bren.setQuality(rs.getString(4));
				bren.setDiamond(rs.getString(5));
				bren.setSize(rs.getInt(6));
				bren.setLettering(rs.getString(7));
				bren.setArtText(rs.getInt(8));
				list.add(bren);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int add(shopping s) {
		int count =0;
		String sql="INSERT INTO shopping(name,price,quality,diamond,size,lettering,artText) VALUES (?,?,?,?,?,?,?) ";
		Object [] pare= {s.getName(),s.getPrice(),s.getQuality(),s.getDiamond(),s.getSize(),s.getLettering(),s.getArtText()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int update(shopping s) {
		int count =0;
		String sql=" UPDATE shopping set name=?,price=?,`quality`=?,diamond=?,size=?,lettering=?,artText=? WHERE shoppingId=?  ";
		Object [] pare= {s.getName(),s.getPrice(),s.getQuality(),s.getDiamond(),s.getSize(),s.getLettering(),s.getArtText()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int deleteById(int id) {
		int count=0;
		String sql="delete from shopping where shoppingId=?";
		Object[] params= {id};
		count=this.executeUpdate(sql, params);
		return count;
	}

}
